package cn.sql_session;

import com.alibaba.druid.pool.DruidDataSource;
import com.mysql.cj.jdbc.Driver;

import javax.sql.DataSource;


/**
 * 类简介: 数据源构建
 *
 * @Author: ShangGuan
 * @Time: 2024/05/22
 **/
public class DataSourceBuild {
    private final String url;
    private final String username;
    private final String password;

    private Driver driver;

    private DataSource dataSource;

    public DataSource getDataSource() {
        return this.dataSource;
    }

    /**
     * 构建DataSource
     * 方法简介:
     * 业务背景:
     *
     * @return {@link DataSource }
     **/
    private DataSource build() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriver(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }

    private DataSourceBuild(String url, String username, String password, Driver driver) {
        this.url = url;
        this.username = username;
        this.password = password;
        this.driver = driver;
        dataSource = build();

    }

    private DataSourceBuild(String url, String username, String password) {
        this.url = url;
        this.username = username;
        this.password = password;
    }

    public static DataSourceBuild dataSourceBuild(String url, String username, String password, Driver driver) {
        return new DataSourceBuild(url, username, password, driver);
    }

    public static DataSourceBuild dataSourceBuild(String url, String username, String password) {
        return new DataSourceBuild(url, username, password);
    }
}
